<?php
$db = new Database();

if(isset($_POST['Action'])){
    $auth = Auth::getAuth('current_user');
    $userID = $auth['id'];
    $timezone = "Asia/Manila";
    if(function_exists('date_default_timezone_set')) date_default_timezone_set($timezone);
    if($_POST['Action']=="updateInvoice"){
	$newInvoice = $_POST["newInvoice"];
	$con = 1;
	$fields = array("UpdateInvoice"=>$newInvoice);
	$conditions  = array("id"=>$con);
	$db->update("tbsettings",$fields,$conditions);
	echo "Record was successfully updated.";
	$audit = array(
			"user_id"=>$userID,
			"action_id"=>13,
			"affected_table"=>"tbsettings",
			"affected_record"=>"updateInvoice",
			"datetime"=>date("Y-m-d  H:i:s"),
			"is_active"=>1
			);
	$db->insert("tbaudit_trail",$audit);
    }elseif($_POST['Action']=="updateRewardPath"){
	$newInvoice = $_POST["newPath"];
	$con = 1;
	$fields = array("UpdaterewardPoints"=>$newInvoice);
	$conditions  = array("id"=>$con);
	$db->update("tbsettings",$fields,$conditions);
	echo "Record was successfully updated.";
	$audit = array(
			"user_id"=>$userID,
			"action_id"=>25,
			"affected_table"=>"tbsettings",
			"affected_record"=>"updateInvoice",
			"datetime"=>date("Y-m-d  H:i:s"),
			"is_active"=>1
			);
	$db->insert("tbaudit_trail",$audit);
    }elseif($_POST['Action']=="updateProduct"){
	$newInvoice = $_POST["newProduct"];
	$con = 1;
	$fields = array("UpdateProduct"=>$newInvoice);
	$conditions  = array("id"=>$con);
	$db->update("tbsettings",$fields,$conditions);
	echo "Record was successfully updated.";
	$audit = array(
			"user_id"=>$userID,
			"action_id"=>14,
			"affected_table"=>"tbsettings",
			"affected_record"=>"UpdateProduct",
			"datetime"=>date("Y-m-d  H:i:s"),
			"is_active"=>1
			);
	$db->insert("tbaudit_trail",$audit);
    }elseif($_POST['Action']=="updateSystemName"){
	$newSystemName = $_POST["systemName"];
	$con = 1;
	$fields = array("SystemName"=>$newSystemName);
	$conditions  = array("id"=>$con);
	$db->update("tbsettings",$fields,$conditions);
	echo "Record was successfully updated.";
	$audit = array(
			"user_id"=>$userID,
			"action_id"=>11,
			"affected_table"=>"tbsettings",
			"affected_record"=>"SystemName",
			"datetime"=>date("Y-m-d  H:i:s"),
			"is_active"=>1
			);
	$db->insert("tbaudit_trail",$audit);
    }elseif($_POST['Action']=="updateFooter"){
	$newSystemName = $_POST["footer"];
	$con = 1;
	$fields = array("footer"=>$newSystemName);
	$conditions  = array("id"=>$con);
	$db->update("tbsettings",$fields,$conditions);
	echo "Record was successfully updated.";
	$audit = array(
			"user_id"=>$userID,
			"action_id"=>12,
			"affected_table"=>"tbsettings",
			"affected_record"=>"footer",
			"datetime"=>date("Y-m-d  H:i:s"),
			"is_active"=>1
			);
	$db->insert("tbaudit_trail",$audit);
    }elseif($_POST['Action']=="updateTime"){
	$newTime = $_POST["time"];
	if(!is_numeric($newTime)){
	    echo "Not a number.";
	}else{
	    
	
	$con = 1;
	$fields = array("Time"=>$newTime);
	$conditions  = array("id"=>$con);
	$db->update("tbsettings",$fields,$conditions);
	echo "Time was successfully updated";
	
	$audit = array(
			"user_id"=>$userID,
			"action_id"=>19,
			"affected_table"=>"tbsettings",
			"affected_record"=>"update",
			"datetime"=>date("Y-m-d  H:i:s"),
			"is_active"=>1
			);
	$db->insert("tbaudit_trail",$audit);
	}
    }elseif($_POST['Action']=="dealerRewardPoints"){
	$val = $_POST['val'];
	$con = 1;
	$fields = array("enabled"=>$val);
	$conditions  = array("id"=>$con);
	$db->update("tbsettings",$fields,$conditions);
	
	$audit = array(
			"user_id"=>$userID,
			"action_id"=>24,
			"affected_table"=>"tbsettings",
			"affected_record"=>"update",
			"datetime"=>date("Y-m-d  H:i:s"),
			"is_active"=>1
			);
	$db->insert("tbaudit_trail",$audit);
    }elseif($_POST['Action']=="updateRedeemFiles"){
	$newrFiles = $_POST["newrFiles"];
	$con = 1;
	$fields = array("exportRedeem"=>$newrFiles);
	$conditions  = array("id"=>$con);
	$db->update("tbsettings",$fields,$conditions);
	echo "Record was successfully updated.";
	$audit = array(
			"user_id"=>$userID,
			"action_id"=>13,
			"affected_table"=>"tbsettings",
			"affected_record"=>"updateInvoice",
			"datetime"=>date("Y-m-d  H:i:s"),
			"is_active"=>1
			);
	$db->insert("tbaudit_trail",$audit);
    }
}
